<?php

namespace app\admin\model\cms;

use app\admin\model\InvestmentCategoryModel;
use app\admin\model\User;
use app\admin\model\UserGroup;
use app\common\model\ChartParent;
use think\Model;
use traits\model\SoftDelete;
//用户付费会员(资源)时长表
class MembershipExpiryRecordModel extends Model
{

    // 表名
    protected $name = 'membership_expiry_record';

    //软删除
    use SoftDelete;

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    //自动写入时间戳的格式
    protected $dateFormat = 'Y-m-d H:i:s';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'expiration_time_text',
        'goods_type_name',
        'goods_name',
        'user_nickname',
        'order_num',
        'table_name_text',
        'membership_duration_text',
    ];

    //回去会员到期天数
    public function getMembershipDurationTextAttr($value,$data){
        if ($data['membership_duration'] === 0){
            return '无限天';
        }
        return $data['membership_duration'];
    }

    //获取商品来源表名称
    public function getTableNameTextAttr($value,$data){
        $table_name_text = '';
        switch ($data['table_name']) {
            case 'plate':
                 $table_name_text = '债市论坛分类';
                break;
            case 'plate_post':
                 $table_name_text = '债市论坛文章';
                break;
            case 'plaza_category':
                 $table_name_text = '首页广场分类';
                break;
            case 'plaza_post':
                 $table_name_text = '首页广场文章';
                break;
            case 'live_category':
                 $table_name_text = '直播分类';
                break;
            case 'live':
                 $table_name_text = '直播';
                break;
            case 'video_category':
                 $table_name_text = '视频分类';
                break;
            case 'video':
                 $table_name_text = '视频';
                break;
            case 'user_group':
                 $table_name_text = '会员组';
                break;
            case 'album':
                 $table_name_text = '专辑';
                break;
            case 'chart_parent':
                $table_name_text = '量化辅助分类';
                break;
            case 'pro_diyform':
                $table_name_text = '量化辅助内容';
                break;
            case 'investment_category':
                $table_name_text = '数据库分类';
                break;
        }

        return $table_name_text;
    }

    //获取商品名称
    public function getGoodsNameAttr($value,$data){
        $goods_name = '';
        switch ($data['table_name']) {
            case 'plate':
                $goods_name = Plate::where('id',$data['goods_id'])->value('platename');
                break;
            case 'plate_post':
                $goods_name = Platepost::where('id',$data['goods_id'])->value('postcontent');
                break;
            case 'plaza_category':
                $goods_name = PlazaCategorymodel::where('id',$data['goods_id'])->value('platename');
                break;
            case 'plaza_post':
                $goods_name = PlazaPostModel::where('id',$data['goods_id'])->value('postcontent');
                break;
            case 'live_category':
                $goods_name = LiveCategoryModel::where('id',$data['goods_id'])->value('platename');
                break;
            case 'live':
                $goods_name = Live::where('id',$data['goods_id'])->value('title');
                break;
            case 'video_category':
                $goods_name = VideoCategoryModel::where('id',$data['goods_id'])->value('platename');
                break;
            case 'video':
                $goods_name = Video::where('id',$data['goods_id'])->value('title');
                break;
            case 'user_group':
                $goods_name = UserGroup::where('id',$data['goods_id'])->value('name');
                break;
            case 'album':
                $goods_name = AlbumModel::where('id',$data['goods_id'])->value('name');
                break;
            case 'chart_parent':
                $goods_name = ChartParent::where('id',$data['goods_id'])->value('name');
                break;
            case 'pro_diyform':
                $goods_name = Diyform::where('id',$data['goods_id'])->value('name');
                break;
            case 'investment_category':
                $goods_name = InvestmentCategoryModel::where('id',$data['goods_id'])->value('platename');
                break;
        }
        if (mb_strlen($goods_name) > 10){
            $goods_name = mb_substr(strip_tags($goods_name), 0, 10) . '...';
        }else{
            $goods_name = mb_substr(strip_tags($goods_name), 0, 10) . '...';
        }

        return $goods_name;
    }

    //获取我方订单号
    public function getOrderNumAttr($value,$data){
        return OrderModel::withTrashed()->where('id',$data['order_id'])->value('order_num');
    }

    public function getUserNicknameAttr($value,$data){
        return User::withTrashed()->where('id',$data['user_id'])->value('nickname');
    }

    public function getGoodsTypeNameAttr($value, $data)
    {
        $value = $value ? $value : $data['goods_type'];
        if ($value == 'categories'){
            $name = '栏目';
        }elseif($value == 'album'){
            $name = '专辑';
        }else{
            $name = '文章';
        }
        return !empty($name) ? $name:'';
    }

    public function getExpirationTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['expiration_time']) ? $data['expiration_time'] : '');
        if ($value){
            return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
        }else{
            return '';
        }
    }

    protected function setExpirationTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    //多对一关联用户
    public function user(){
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    //一对一关联订单
    public function order()
    {
        return $this->hasOne(OrderModel::class,'id','order_id');
    }

}
